package com.platform.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.ConfigArgs;
import com.platform.entity.UserOrder;
import com.platform.mapper.UserOrderMapper;

@Service
public class OrderService extends ServiceImpl<UserOrderMapper, UserOrder> {

	@Resource
	private UserOrderMapper orderMapper;
	@Autowired
	private ConfigArgs configArgs;

	public void seeAllUnPay() throws AlipayApiException {
		List<UserOrder> list = orderMapper.seeAllUnPay("待付款");
		for (UserOrder o : list) {

			AlipayClient alipayClient = new DefaultAlipayClient(configArgs.getAliPayUrl(), configArgs.getAppId(),
					configArgs.getAppPrivateKey(), "json", "utf-8", configArgs.getAlipayPublicKey(), "RSA2");
			// AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

			// AlipayClient alipayClient = new
			// DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your
			// private_key","json","GBK","alipay_public_key","RSA2");
			AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

			JSONObject bizContent = new JSONObject();
			// bizContent.put("out_trade_no", "20150320010101001");
			bizContent.put("out_trade_no", o.getOrderNo());
			// JSONArray queryOptions = new JSONArray();
			// queryOptions.add("trade_settle_info");
			// bizContent.put("query_options", queryOptions);

			request.setBizContent(bizContent.toJSONString());

			AlipayTradeQueryResponse response = alipayClient.execute(request);
			JSONObject jsonObject = JSONObject.parseObject(response.getBody());
			JSONObject js = jsonObject.getJSONObject("alipay_trade_query_response");
			if (response.isSuccess()) {
				System.out.println("调用成功");
				if (js.getString("code").equals("10000") && js.getString("trade_status").equals("TRADE_SUCCESS")) {
					orderMapper.updatePay(o.getId(), "待发货");
				}
			} else {
				// 待支付大于30分钟改成取消
				if (js.getString("code").equals("40004") && js.getString("sub_code").equals("ACQ.TRADE_NOT_EXIST")) {
					// 定义日期时间格式
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
					// 将字符串时间解析为 LocalDateTime 对象
					LocalDateTime datetimeTime = LocalDateTime.parse(o.getCreateTime(), formatter);
					// 获取当前系统时间
					LocalDateTime currentTime = LocalDateTime.now();
					// 计算两者之间的时间差
					long minutesDiff = ChronoUnit.MINUTES.between(datetimeTime, currentTime);
					// 判断时间差是否在 30 分钟以内
					if (minutesDiff > 30) {
						orderMapper.updatePay(o.getId(), "已取消");
					}

				}

			}
		}
	}
}